package com.task.controller.system;

import com.task.common.annotation.Log;
import com.task.common.entity.sys.model.role.RoleCreateModel;
import com.task.common.entity.sys.model.role.RoleQueryModel;
import com.task.common.entity.sys.model.role.RoleUpdateModel;
import com.task.common.entity.sys.Role;
import com.task.common.enums.BusinessType;
import com.task.common.util.body.JsonResult;
import com.task.common.util.body.ResultTool;
import com.task.common.util.model.ModelUtil;
import com.task.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Huzz
 * @created 2021-12-06 16:04
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Log(title = "操作日志", businessType = BusinessType.INSERT, isSaveResponseParam = true)
    @ApiOperation(value = "添加角色")
    @PostMapping("/addRole")
    public JsonResult addRole(@RequestBody RoleCreateModel model){
        return roleService.addRole((Role) ModelUtil.toEntityStatic(model, Role.class)) ? ResultTool.success() : ResultTool.fail();
    }

    @Log(title = "操作日志", businessType = BusinessType.UPDATE, isSaveResponseParam = true)
    @ApiOperation(value = "根据id修改角色信息")
    @PutMapping("/updateRoleById")
    public JsonResult updateRoleById(RoleUpdateModel model){
        return roleService.updateRoleById((Role) ModelUtil.toEntityStatic(model, Role.class)) ? ResultTool.success() : ResultTool.fail();
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE, isSaveResponseParam = true)
    @ApiOperation(value = "删除角色信息")
    @DeleteMapping("/delRoleById")
    public JsonResult delUserById(Integer roleId){
        return roleService.delRoleById(roleId) ? ResultTool.success() : ResultTool.fail("删除失败, 请检查是否存在数据关联");
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查询分页列表")
    @GetMapping("/finRoleListPage")
    public JsonResult finUserRolePage(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                      @RequestParam(defaultValue = "10", required = false) Integer pageSize,
                                      RoleQueryModel model){
        return ResultTool.success(roleService.finRolePage(pageNum, pageSize, (Role) ModelUtil.toEntityStatic(model, Role.class)));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据id进行查询")
    @GetMapping("/finUserById")
    public JsonResult finRoleById(Integer roleId){
        return ResultTool.success(roleService.finRoleById(roleId));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据角色名进行查询")
    @GetMapping("/finUserByRoleName")
    public JsonResult finUserByRoleName(String roleName){
        return ResultTool.success(roleService.finUserByRoleName(roleName));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "角色分配")
    @GetMapping("/roleDistribute")
    public JsonResult roleDistribute(@RequestParam Integer roleId, @RequestParam Integer userId){
        return ResultTool.success(roleService.roleDistribute(roleId, userId));
    }
}
